<?php


namespace App\Http\Controller;


use App\Helper\RedisConst;
use App\Model\Logic\InviteCodeLogic;
use App\Model\Logic\LimitNumCheckLogic;
use App\Model\Logic\MemberLogic;
use App\Model\Logic\OauthLogic;
use App\Model\Logic\TimeZoneLogic;
use App\Model\Logic\YunXinLogic;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Console\Helper\Show;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Redis\Pool;
use Swoft\Task\Task;
use Swoft\Validator\Annotation\Mapping\Validate;

/**
 * @Bean()
 * @Controller("passport")
 * Class PassportController
 * @package App\Http\Controller
 */
class PassportController extends BaseController
{
    /**
     * @Inject()
     * @var YunXinLogic
     */
    private $yunxin;
    /**
     * @Inject()
     * @var InviteCodeLogic
     */
    private $invite_logic;
    /**
     * @Inject()
     * @var OauthLogic
     */
    private $oauth_logic;

    /**
     * @Inject()
     * @var TimeZoneLogic
     */
    private $time_zone_logic;
    /**
     * @Inject()
     * @var MemberLogic
     */
    private $member_logic;

    /**
     * @Inject()
     * @var LimitNumCheckLogic
     */
    private $limit_num_check;

    /**
     * @Inject()
     * @var Pool
     */
    private $redis;
    /**
     *
     * 第三方登录
     * @RequestMapping(route="login", method={RequestMethod::GET})
     * @param Request $request
     * @return PassportController
     */
    public function oauthLogin(Request $request)
    {
//        sgo(function (){
//            $data = $this->redis->hGetAll(RedisConst::MEMBER . '987');
//        });
        $data = $this->redis->hGetAll(RedisConst::MEMBER . '987');
        $data = [];
//        $account = $request->post("account");
//        $password = $request->post("password");
//        $data = [$account, $password];
        return $this->response(10000, $data);
    }

    /**
     * 登录
     * @RequestMapping("/sign/in", method={RequestMethod::POST})
     * @Validate(validator="AppValidator",fields={"account", "password"})
     */
    public function signIn(Request $request)
    {
        $account = strtolower($request->post("account"));
        $password = $request->post("password");

        if(isEmail($account)){
            $condition['email'] = $account;
        }else{
            $condition['nickname'] = $account;
        }
        $member_info = $this->member_logic->findOne($condition);
        if(!$member_info) return $this->response(10004);
        //密码检测
        $pwd_check = $this->member_logic->passwordCheck($account, $password, $member_info['password']);
        if(is_numeric($pwd_check)) return $this->response($pwd_check);
        //获取最后登录角色账户 array || null
        $is_business = false;
        $login_id = $member_info['id'];
        $last_login = $this->member_logic->getLastRoleByNormalId($member_info['id']);
        if($last_login && $last_login['type'] != 'normal'){
            $is_business = true;
            $login_id = $last_login['member_id'];
        }
        $result = $this->member_logic->memberLoginInfo($login_id);
        //设备登录账户数量限制
        $device_limit_check = $this->limit_num_check->userDeviceDailyBind($request->serial_number, $result['member_id'], $request->lang);
        if(is_numeric($device_limit_check)) return $this->response($result);

        $result['share_text_limit'] = config('app.share_text_limit');
        $result['comment_text_limit'] = config('app.comment_text_limit');
        $result['post_text_limit'] = config('app.post_text_limit');
        $result['yunxin'] = $this->yunxin->yunXinID($result['member_id']);
        $result['invite_code'] = $this->invite_logic->getInviteCodeByMemberId($is_business ? $last_login['member_id'] : $member_info['id']);
        $result['normal_member_id'] = $member_info['id'];
        $result['normal_member_uuid'] = $member_info['uuid'];
        $result['role'] = $is_business ? $last_login['type'] : 'normal';
        $result['business_id'] = $is_business ? $last_login['business_id'] : 0;
        $result['business_account'] = $is_business ? 1 : $this->member_logic->getBusinessListByNormal($member_info['id'], $request->version, false) ? 1 : 0;
        //获取用户 的 key 和 秘钥
        $result['key_secrect'] = $this->oauth_logic->keySecret($member_info['id'], $request->serial_number, 'update');
        //用户是否有发布review 的权限
        $result['review_permission'] = 0;
        if($result['review_permission'] || $result['role'] != 'normal'){
            $result['review_permission'] = 1;
        }
        $result['tik'] = $this->oauth_logic->mkTik($member_info['id'], $request);
        $result['mix_secret'] = $this->oauth_logic->setMixSecret($request->serial_number, $result['member_uuid']);
        $result['member_timezone'] = $this->time_zone_logic->getMemberTimeZoneRedis($member_info['id']) == '-24' ? 0 : 1;

        //用户登录日志
        $forwarded_for_ip = $request->getHeaderLine('x-forwarded-for');
        $forwarded_for_ip = explode(',', $forwarded_for_ip);
        //异步投递任务
        Task::async('baseTask', 'loginLog', [ $result['member_id'], $request->device, $request->serial_number, $forwarded_for_ip[0], $request->version ]);
//        return $this->response(10000);
        return $this->response(10000, $result);
    }

    /**
     * 注册
     * @RequestMapping("/sign/up", method={RequestMethod::POST})
     */
    public function signUp()
    {
        return $this->response();
    }

    /**
     * @RequestMapping("/member/logout", method={RequestMethod::POST})
     */
    public function logout()
    {
        return $this->response();
    }

}